//
//  DWDVc.swift
//  GjCup
//
//  Created by abc on 2024/4/7.
//

import UIKit
enum DWType: Int {
    case wd = 0
    case cd = 1
    case mj
    case tj
    case zl
    case yq
    case gl
    case gnr
    
}

class DWDVc: UIViewController {
    
    var type: DWType = .wd{
        didSet{
            let titles = ["温度","长度","面积","体积","质量","压强","功率","功能/热"]

            navigationItem.title = titles[type.rawValue]
        }
    }
    var typeRawvalue: Int = 0{
        didSet{
            cBtn.setTitle(titles[typeRawvalue], for: .normal)
        }
    }
    
    var titles: [String]{
        get{
            switch type {
            case .wd:
                let m = WdModel()
                return m.wdTypes
            case .cd:
                let m = CdModel()
                return m.cdTypes
            case .mj:
                let m = MjModel()
                return m.mjTypes
            case .tj:
                let m = TjModel()
                return m.types

            case .zl:
                let m = ZlModel()
                return m.types
            case .yq:
                let m = YqModel()
                return m.types
            case .gl:
                let m = GlModel()
                return m.types
            case .gnr:
                let m = GNRModel()
                return m.types
            }
        }
    }
    var values: [String] = []
    
    
    @IBOutlet weak var tf: UITextField!
    
    @IBOutlet weak var cBtn: UIButton!{
        didSet{
            cBtn.layer.cornerRadius = 6
        }
    }
    
    @IBOutlet weak var beginBtn: UIButton!{
        didSet{
            beginBtn.layer.cornerRadius = 6
        }
    }
    @IBOutlet weak var tView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        cBtn.setTitle(titles[typeRawvalue], for: .normal)
    }
    
    @IBAction func beginAc(_ sender: UIButton) {
        view.endEditing(true)
        let text = tf.text ?? ""
        if text.isEmpty {
            
            UIAlertController.ok_alert("请输入数值", self)
            return
        }
        switch type {
        case .wd:
            if let h = Float(text) {
                let m = WdModel()
                values = m.allChange(t: h, type: WdType(rawValue: typeRawvalue) ?? .C)
            }

            break
        case .cd:
            if let h = Double(text) {
                let m = CdModel()
                values = m.allChange(n: h, type: CdType(rawValue: typeRawvalue) ?? .km)
            }
        case .mj:
            if let h = Double(text) {
                let m = MjModel()
                values = m.allChange(d: h, type: MJType(rawValue: typeRawvalue) ?? .km2)
            }
        case .tj:
            if let h = Double(text) {
                let m = TjModel()
                values = m.allChange(d: h, type: TjType(rawValue: typeRawvalue) ?? .km3)
            }
        case .zl:
            if let h = Double(text) {
                let m = ZlModel()
                values = m.allChange(d: h, type: ZlType(rawValue: typeRawvalue) ?? .kg)
            }
        case .yq:
            if let h = Double(text) {
                let m = YqModel()
                values = m.allChange(d: h, type: YqType(rawValue: typeRawvalue) ?? .pa)
            }
        case .gl:
            if let h = Double(text) {
                let m = GlModel()
                values = m.allChange(d: h, type: GlType(rawValue: typeRawvalue) ?? .W)
            }
        case .gnr:
            if let h = Double(text) {
                let m = GNRModel()
                values = m.allChange(d: h, type: GNRType(rawValue: typeRawvalue) ?? .J)
            }



            
        }
        tView.reloadData()
        
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        view.endEditing(true)
    }

    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
        if segue.identifier == "DWDToC" {
            let vc = segue.destination as! DWCVc
            vc.titles = titles
            vc.block = {[weak self] (index) in
                self?.typeRawvalue = index
            }
        }
    }
    

}
extension DWDVc:UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DWDCell", for: indexPath) as! DWDCell
        cell.nameLa.text = titles[indexPath.row]
        cell.valueLa.text = " " + values[indexPath.row] + "   "
        return cell
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return values.count
    }
}
